package com.nearby.android;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity{
	private TextView cpustatus;
	private TextView memstatus;
	
	private SharedPreferences preference;
	private SharedPreferences.Editor editor;
	
	private String serverIp;
	private String serverPort;
	
	private Handler handler;
	
	final int SETTING_MENU = 1;
	final int EXIT_MENU = 2;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.index);
		
		cpustatus = (TextView) findViewById(R.id.cpustatus);
		memstatus = (TextView) findViewById(R.id.memstatus);
		
		//getServerStatus();
	}
	public boolean onCreateOptionsMenu(Menu menu){
		MenuItem setting = menu.add(0, SETTING_MENU, 1, "设置");
		MenuItem exit = menu.add(0, EXIT_MENU, 2, "退出");
		
		setting.setIcon(R.drawable.setting);
		
		exit.setIcon(R.drawable.exit);
		
		return super.onCreateOptionsMenu(menu);
	}
	public boolean onOptionsItemSelected(MenuItem mi){
		switch(mi.getItemId()){
		case EXIT_MENU:
			Toast.makeText(this, "你点击了退出按钮", Toast.LENGTH_SHORT).show();
			break;
		case SETTING_MENU:
			Intent setting = new Intent(this, Setup.class);
			startActivity(setting);
			break;
		}
		return true;
	}
	/*
	 * 获取服务端状态
	 */
	public void getServerStatus(){
		//获取服务端ip和端口
		preference = getSharedPreferences("com.nearby.android", MODE_PRIVATE);  //只能本应用程序读写
		serverIp = preference.getString("serverip", "192.168.49.130");
		serverPort = preference.getString("serverport", "8888");
		
		if(serverIp == null || serverPort == null){
			Toast toast = Toast.makeText(this, "请设置服务器ip和端口", Toast.LENGTH_LONG);
			toast.show();
		}
		
		handler = new Handler(){
			public void handleMessage(Message msg){
				if(msg.what == 1){
					try {
						Socket socket = new Socket(serverIp, Integer.valueOf(serverPort));
						BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
						String text = reader.readLine();
						cpustatus.setText(text);
						memstatus.setText(text);
						
					} catch (NumberFormatException e) {
						e.printStackTrace();
					} catch (UnknownHostException e) {
						e.printStackTrace();
					} catch (IOException e) {
						Log.i("test", e.getMessage());
						e.printStackTrace();
					}
				}
				super.handleMessage(msg);
			}
		};
		
		new Thread(new MyThread()).start();
	}
	
	class MyThread  implements Runnable{
		@Override
		public void run() {
			while(true){
				try {
//					Log.i("test", "lijianwei");
					Thread.sleep(1000);
					Message msg = new Message();
					msg.what = 1;
					handler.sendMessage(msg);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
